package com.ssh;

import org.junit.Test;

public class SearchRange_34 {
	public int[] searchRange(int[] nums, int target) {
		int[] a = {-1, -1};
		int n = nums.length-1;
		if (n == -1) {
			return a;
		}
		a[0] = b(nums, 0, n, target);
		if (a[0] == -1) {
			return a;
		}
		int i = a[0]+1;
		for (; i <= n; i++) {
			if (nums[i] != target) {
				break;
			}
		}
		a[1] = i-1;
		int j = a[1]-1;
		for (; j >=0; j--) {
			if (nums[j] != target) {
				break;
			}
		}
		a[0] = j+1;
        return a;
    }
	public int b (int[] nums, int l, int r, int target) {
		if (r < l) {
			return -1;
		}
		int mid = l + (r - l) / 2;
		if (nums[mid] < target) {
			return b (nums, mid+1, r, target);
		}else if (nums[mid] > target) {
			return b (nums, l, mid-1, target);
		}else {
			return mid;
		}
	}
	@Test
	public void test () {
		int[] a = {};
		int[] b = searchRange(a, 2);
		System.out.println(b[0]+"---="+b[1]);
		
	}
}
